home *** CD-ROM | disk | FTP | other *** search
- #define Uses_ifpstream
- #define Uses_ofpstream
- #define Uses_TStringList
- #define Uses_TStrListMaker
- #define Uses_Strings
- #include <tv.h>
- #include "strings.h"
- #include <mem.h>
- #include <string.h>
- #include <io.h>
- #include <share.h>
- #include <fcntl.h>
-
- __link(RStringList)
-
- Strings::Strings() :
- items(0),
- count(0),
- slist(0),
- is(0)
- {
- }
-
- Strings::Strings( Strings& s ) :
- items(0),
- count(0),
- slist(0),
- is(0)
- {
- if( s.items ) // s contains array of pointers to StrRef.
- {
- items = new StrRef *[s.count];
- if( items == 0 )
- return;
- memcpy( items, s.items, s.count*sizeof(StrRef *) );
- count = s.count;
- }
- else if( s.slist ) // s uses string resource file.
- load( s.fname );
- }
-
- Strings::~Strings()
- {
- clear();
- }
-
- char *Strings::get( ushort id )
- {
- int n;
-
- if( items ) // uses StrRef pointer array.
- {
- if( search(id, n) )
- return items[n]->str;
- else
- return 0;
- }
- else if( slist ) // uses string resource file.
- {
- slist->get( buf, id );
- if( *buf )
- return buf;
- else
- return 0;
- }
- else // empty.
- return 0;
- }
-
- void Strings::clear()
- {
- if( items )
- delete items;
- items = 0;
- count = 0;
-
- if( slist )
- delete slist;
- slist = 0;
- if( is )
- delete is;
- is = 0;
- }
-
- Boolean Strings::load( StrRef *arg )
- {
- clear();
- if( arg == 0 ) return False;
-
- int limit, u, n;
- for( limit = 0; arg[limit].id != srNull; limit++ );
- items = new StrRef *[limit];
- if( items == 0 )
- return False;
- for( u = 0; u < limit; u++ )
- {
- search( arg[u].id, n );
- if( n < count )
- memmove( items+n+1, items+n, (count-n)*sizeof(StrRef *) );
- items[n] = &arg[u];
- count++;
- }
- return True;
- }
-
- Boolean Strings::load( char *fspec )
- {
- clear();
- strcpy( fname, fspec );
- int handle = open( fname, O_RDONLY | SH_DENYWR | O_BINARY );
- if( handle < 0 )
- return False;
- is = new ifpstream;
- if( is == 0 )
- {
- close(handle);
- return False;
- }
- is->attach(handle);
- if( !(*is) )
- {
- close(handle);
- delete is;
- return False;
- }
- *is >> slist;
- return True;
- }
-
- Boolean Strings::store( char *fspec )
- {
- if( items == 0 )
- return False;
-
- long len, i, n;
-
- len = 0L;
- for( i = 0; i < count; i++ )
- len += strlen(items[i]->str)+1;
- len += 10; // buffer
- if( len > 0xFFFFL )
- return False; // too big
- TStrListMaker *lm = new TStrListMaker( (ushort)len, count+2 );
- if( !lm ) return False;
- for( i = 0; i < count; i++ )
- lm->put( items[i]->id, items[i]->str );
-
- ofpstream os( fspec );
- if( !os )
- {
- os.close();
- delete lm;
- return False;
- }
- os << lm;
- os.close();
- delete lm;
- return True;
- }
-
- Boolean Strings::search( ushort id, int& pos )
- {
- Boolean found = False;
- if( !count )
- pos = 0;
- else
- {
- int l = 0;
- int h = count - 1;
- while( l <= h )
- {
- int i = (l + h) >> 1;
- if( items[i]->id < id )
- l = i + 1;
- else if( items[i]->id > id )
- h = i - 1;
- else
- {
- found = True;
- l = i;
- h = i-1;
- }
- }
- pos = l;
- }
- return found;
- }
-